package com.web.Controller;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.log4j.Logger;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.upload.UploadFile;

public class PublicController extends Controller {
	private static Logger logger = Logger.getLogger(HomeController.class);
	public void index() {
		// UploadFile uf = this.getFile();
		renderText("ok");
	}
	
	public void uploads() {
		// 获取上传的文件
		//创建日期文件夹并重命名文件
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("HHmmss");
		Date now=new Date();
		String nowdate = dateFormat1.format(now);
		String nowtime = dateFormat2.format(now);

		int maxfile = 200 * 1024 * 1024;
		UploadFile uf = getFile("upfile","",maxfile, "UTF-8");
		Random  random=new Random();
		int randInt=random.nextInt(100);// 返回[0,100)集合中的整数，注意不包括100
		File oldfile=uf.getFile(); 
		String oldstr= oldfile.getPath();
		logger.info("//文件物理路径旧路径"+oldstr);
		String fileNew=uf.getUploadPath()+randInt+nowdate+""+nowtime+uf.getFileName();
		logger.info("fileNew+++"+fileNew);
		File newfile=new File(fileNew); 
        oldfile.renameTo(newfile); 
        logger.info("新路径"+newfile.getPath());
		
		renderJson("{\"status\":200,\"msg\":\"upload/"+newfile.getName()+"\"}");
	}
}

